package com.bingxue.edu.framework.web;

import com.bingxue.edu.framework.enums.DictEnum;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterFactory;

@SuppressWarnings("rawtypes")
public class DictEnumConverterFactory implements ConverterFactory<String, DictEnum> {

    @Override
    public <T extends DictEnum> Converter<String, T> getConverter(Class<T> targetType) {
        T[] enums = targetType.getEnumConstants();
        return source -> {
            for (T e : enums) {
                if (e.getValue().toString().equals(source)) {
                    return e;
                }
            }
            throw new IllegalArgumentException("无法匹配对应的枚举类型: " + source);
        };
    }
}
